<template>
     <el-form :model="tableForm" :rules="tableRule" ref="tableForm" label-width="120px">


       <el-form-item label="评估分数" prop="score">
         <el-input size="small" v-model="tableForm.score" auto-complete="off" disabled placeholder="请输入评估分数"></el-input>
       </el-form-item>

       <el-form-item label="评估结果" prop="result">
         <el-input size="small" type="textarea" v-model="tableForm.result" disabled auto-complete="off" placeholder="请输入评估结果"></el-input>
       </el-form-item>

       <el-form-item label="营养指导" prop="nutrition">
         <el-input size="small" type="textarea" v-model="tableForm.nutrition" auto-complete="off" placeholder="请输入营养指导"></el-input>
       </el-form-item>

       <el-form-item label="运动建议" prop="support">
         <el-input size="small" type="textarea" v-model="tableForm.support" auto-complete="off" placeholder="请输入运动建议"></el-input>
       </el-form-item>

       <el-form-item label="用药方案" prop="way">
         <el-input size="small" type="textarea" v-model="tableForm.way" auto-complete="off" placeholder="请输入用药方案"></el-input>
       </el-form-item>

     </el-form>
</template>

<script>

import {addData, updateData, getPhysicalData, getPharmacyData, getMedicalData, getScoreData} from '@/api/assess'
import Selector from '../user/selector'

// 表单校验
const tableRule = {
  account: [{ required: true, message: '请输入卡号', trigger: 'blur' }],
  score: [{ required: true, message: '请输入评估分数', trigger: 'blur' }],
};

// 请求数据用户
const searchForm = {
  currentPage: 1,
  pageSize: 200,
  userId: '',
  account: ''
}

export default {
  name: 'MeddleEdit',
  props: {
    formData :{
      type: Object
    }
  },
  data() {
    return {
      tableForm: this.formData,
      tableRule: tableRule,
      visible: false,
      searchForm: searchForm,
      physicalData: [],
      pharmacyData: [],
      medicalData: []
    }
  },
  components:{
    Selector
  },
  created () {
    this.searchForm.userId = this.formData.userId
  },
  methods:{
    //表单保存事件
    submitForm() {
      this.$refs["tableForm"].validate(valid => {
        if (valid) {
          if(this.tableForm.id == ""){
            addData(this.tableForm).then(res => {
              if (res.message == "success" ) {
                this.$message({
                  type: 'success',
                  message: '新增成功！'
                });
                this.$emit("closeDialog", false);
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                });
              }
            });
          }else{
            updateData(this.tableForm).then(res => {
              if (res.message == "success" ) {
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                });
                this.$emit("closeDialog", false);
              } else {
                this.$message({
                  type: 'info',
                  message: res.message
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 选择父表数据
    getSelectorData (v) {
      this.visible = false
      this.searchForm.userId = v[0].id

      this.tableForm.account = v[0].account
      this.tableForm.userId = v[0].id
      this.tableForm.name = v[0].name
      this.tableForm.phone = v[0].phone
      this.tableForm.card = v[0].card
      this.tableForm.address = v[0].address
      this.tableForm.medical = v[0].medical
      this.tableForm.allergy = v[0].allergy
      this.tableForm.drug = v[0].drug

      this.refreshData()
    },
    refreshData(){
      getPhysicalData(this.searchForm).then(res => {
        this.physicalData = res.body.records
      })

      getPharmacyData(this.searchForm).then(res => {
        this.pharmacyData = res.body.records
      })

      getMedicalData(this.searchForm).then(res => {
        this.medicalData = res.body.records
      })

      getScoreData(this.searchForm).then(res => {
        let data = res.body
        this.tableForm.score = data.score
        this.tableForm.result = data.result

      })
    },
    clearForm(){
      this.$refs.tableForm.resetFields();
      this.$refs.tableForm.clearValidate();
    }
  }
}
</script>

<style scoped>


</style>
